import { SET_MENUS, CLEAR_MENUS, SELECT_MENU} from '../constants'

import { getCurrentUserMenuList } from '../../api/menu'

// 同步action-creator返回action对象
export const clearMenus = () => ({ type: CLEAR_MENUS })

// 同步action-creator返回action对象
export const selectMenu = (menuId) => ({ type: SELECT_MENU, menuId })

// 异步action-creator返回(dispatch) => {}
export const setMenus = (cb) => {
    return (dispatch) => {
        getCurrentUserMenuList().then(res => {
            if(res.length === 0) {
                dispatch({ type: SET_MENUS, menus: []})
                cb([])
                return
            }
            let menus = [{ menuId: -1, menuName: '首页', parentId: 0, orderNum: 0, path: 'index', component: 'TrapIndex', menuType: 'C', icon: 'HomeOutlined' }, ...res]
            dispatch({ type: SET_MENUS, menus })
            cb(menus)
        })
    }
}